/**
 *
 */
package info.motteke.swt;

import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.GlyphMetrics;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.TextLayout;

/* package */ abstract class ReplaceEntry {

    private final TextLayout layout;

    private final Point margin;

    private final int offset;

    public ReplaceEntry(TextLayout layout, Point margin, int offset) {
        this.layout = layout;
        this.margin = margin;
        this.offset = offset;
    }

    protected abstract void draw(GC gc);

    protected abstract void dispose();

    protected Point getDrawLocation() {
        int lineIndex = layout.getLineIndex(offset);
        FontMetrics lineMetrics = layout.getLineMetrics(lineIndex);
        Point point = layout.getLocation(offset, false);
        GlyphMetrics glyphMetrics = layout.getStyle(offset).metrics;

        return new Point(point.x + margin.x, point.y + margin.y + lineMetrics.getAscent() - glyphMetrics.ascent);
    }
}